package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionProcessDevice;
import com.wing.productionPlan.model.request.ProductionProcessDeviceForm;
import com.wing.productionPlan.model.response.ProductionProcessDeviceVO;
import com.wing.productionPlan.service.ProductionProcessDeviceService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 制造令-Controller
 *
 * @author xiayang
 */
@Validated
@Api(value = "制造令管理", tags = "#/productionPlan/makeOrder")
@RestController("makeOrderController")
@RequestMapping("makeOrder/admin/makeOrder")
@RequiredArgsConstructor
public class MakeOrderController {

    private final ProductionProcessDeviceService productionProcessDeviceService;

    @ApiOperation(value = "查询制造令列表", notes = "查询制造令列表")
    @GetMapping("/list")
    public PageResult<ProductionProcessDeviceVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            Long productionPlanId, Long processId, String deviceCode, String skuCode, String skuName) {
        Page<ProductionProcessDevice> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productionPlanId",productionPlanId);
        params.put("processId",processId);
        params.put("deviceCode",deviceCode);
        params.put("skuCode",skuCode);
        params.put("skuName",skuName);
        IPage<ProductionProcessDeviceVO> list = productionProcessDeviceService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询制造令下拉框列表", notes = "查询制造令下拉框列表")
    @GetMapping("/getSelectList")
    public JsonResult list(Long productionPlanId, Long processId, String deviceCode, String skuCode, String skuName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productionPlanId",productionPlanId);
        params.put("processId",processId);
        params.put("deviceCode",deviceCode);
        params.put("skuCode",skuCode);
        params.put("skuName",skuName);
        List<ProductionProcessDeviceVO> list = productionProcessDeviceService.list4app(params);
//        if (CollectionUtil.isNotEmpty(list)) {
//            //计划数量与已排产数量想同时，就不允许添加开工单
//            list.removeIf(productionProcessDevice -> productionProcessDevice.getOutputQuantity() != null && productionProcessDevice.getPlanProductionQuantity().equals(productionProcessDevice.getOutputQuantity()));
//        }
        return JsonResult.success(list);
    }

    @ApiOperation(value = "制造令详情", notes = "制造令详情")
    @GetMapping("/get")
    public JsonResult get(Long productionProcessDeviceId) {
        return JsonResult.success(productionProcessDeviceService.get4appById(productionProcessDeviceId));
    }
}
